package com.demo.service;

import com.demo.bean.User;
import com.demo.dao.ForumDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ForumService {

    @Autowired
    ForumDao dao;

    //    获取分页列表
    public Map<String,Object> getPageList(Map<String, Object> paramMap) {
        Long totalCount=dao.getTotalCount(paramMap);
        List<Map<String,Object>> data=dao.getMapListByParam(paramMap);

        Map<String,Object>resultMap=new HashMap<>();
        resultMap.put("data",data);
        resultMap.put("count",totalCount);
        resultMap.put("code",0);
        resultMap.put("msg","ok");
        return resultMap;
    }
    //保存数据，自动根据有无id判断是新增还是更新
    public void saveItem(Map item) {
        String id = item.get("id").toString();
        item.put("createtime",new Date());
        if(StringUtils.isEmpty(id)){
            dao.addItem(item);
        }else {
            dao.updateItem(item);
        }
    }


    public List<Map> getAll() {
        return dao.getAll();
    }

    public Map getById(String id) {
        return dao.getById(id);
    }

    public void deleteItemsByIds(Map param) {
        System.out.println(param);
    }
}
